package com.zjl.学习.第03章_步骤Shop.F_流式步骤;

import com.zjl.springbatch.util.OutColour;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.nio.charset.Charset;

/**
 * ClassName: F_流式步骤
 * Package: com.zjl.学习.第03章_步骤Shop.F_流式步骤
 * Description:
 *
 * @Author 张蛟龙
 * @Create 2024/8/3 14:03
 * @Version 1.0
 */
@SpringBootApplication
@EnableBatchProcessing//开启spring batch
public class F_流式步骤 {
    /**
     * ExitStatus.UNKNOWN //未知，不知道的状态
     * ExitStatus.EXECUTING //执行
     *      表示处理时的连续状态仍在发生，因此不需要采取进一步行动。
     *      用于异步执行场景，其中处理发生在另一个线程或进程，调用者不需要等待结果
     * ExitStatus.COMPLETED //已完成的加工
     * ExitStatus.NOOP //表示没有处理的作业
     * ExitStatus.FAILED  //有误差的已完成处理。
     * ExitStatus.STOPPED  //表示已完成的加工 中断状态停止
     * //每个 step 只会执行一遍  即 只会进入一个判断的成立语句（即 start/from），成立过一次后，就不能再次进入to了
     * //from 就是如果   on 就是 from的值是否等于此值  等于的话执行  to
     */
    public static void main(String[] args) {
//        args = new String[]{"name=张蛟龙3"};//模拟 入参
        System.setProperty("spring.profiles.active","01");//  同理 --spring.profiles.active=xxx

        SpringApplication.run(F_流式步骤.class, args);
        System.out.println("***********启动完毕**当前编码:" + Charset.defaultCharset().name() + "****************");

    }
    @Autowired//@EnableBatchProcessing 这个注解帮你从 spring batch 的配置文件中加载了
    private JobLauncher jobLauncher;//Job 启动器
    @Autowired//Job 构造工厂
    private JobBuilderFactory jobBuilderFactory;
    @Autowired//构建 Step
    private StepBuilderFactory stepBuilderFactory;
    @Bean//任务 Tasklet 这是最简单的    还有Chunk块处理
    public Tasklet tasklet03A(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************A*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet03B(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************B*******************");

                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet03C(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************C*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet03B1(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************B1*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet03B2(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************B2*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet03B3(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************B3*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Step step03A() throws Exception {
        return stepBuilderFactory.get("step03A").tasklet(tasklet03A()).build();
    }


    @Bean
    public Step step03B1() throws Exception {

        return stepBuilderFactory.get("step03B1").tasklet(tasklet03B1()).build();
    }
    @Bean
    public Step step03B2() throws Exception {

        return stepBuilderFactory.get("step03B2").tasklet(tasklet03B2()).build();
    }
    @Bean
    public Step step03B3() throws Exception {

        return stepBuilderFactory.get("step03B3").tasklet(tasklet03B3()).build();
    }
    @Bean
    public Flow flow03B() throws Exception {
        return new FlowBuilder<Flow>("flow03B")
                .start(step03B1())
                .next(step03B2())
                .next(step03B3())
                .build();
    }
    @Bean
    public Step step03B() throws Exception {
        //将 B1 B2 B3  都放入 B 里
        return stepBuilderFactory.get("step03B").flow(flow03B()).build();
    }

    @Bean
    public Step step03C() throws Exception {

        return stepBuilderFactory.get("step03C").tasklet(tasklet03C()).build();
    }
    @Bean// 会自己找 指定符合的类传进去入参
    public Job job003Z() throws Exception {
        return jobBuilderFactory.get("job003流式步骤")
                .start(step03A())
                .next(step03B())//B 里面有   B1  B2  B3  三个  和一体
                .next(step03C())
                .build();

    }
}
